<?php

class site_banner_model extends CI_Model {

    protected $_instance = array(
        'table' => 'site_banner',
        'primaryKey' => 'id',
    );

    public $_data = null;

    const TYPE_IMAGE = 1;
    const TYPE_VIDEO = 2;

    protected $_display = array(
        '1' => 'Xuống dòng',
        '2' => 'Căn lề trái',
        '3' => 'Căn lề phải'
    );

    protected $_display_class = array(
        '1' => 'cl',
        '2' => 'bl',
        '3' => 'br'
    );

    protected $_target = array(
        '_blank' => 'Trang mới',
        '_self' => 'Chuyển trang'
    );

    public function get_banners() {
        $query = $this->db->get($this->_instance['table']);
        $list_banner = array();
        foreach ($query->result() as $row) {
            $list_banner[] = $row;
        }
        $query->free_result();
        return $list_banner;
    }

    public function get_collection($start, $limit, $conds = null, $orders = null) {
        $query = 'SELECT bn.zone_id, bn.display, bn.id, bn.alias, bn.target, bn.is_visible, bn.link, bn.name, bn.file, bn.width, bn.height, bn.created_at, sa.username AS uploader FROM ' . $this->_instance['table'] . ' bn LEFT JOIN site_admin sa ON bn.user_id = sa.id';
        if (is_array($conds)) {
            $conds = implode(' AND ', $conds);
        }
        if (!empty($conds)) {
            $query .= ' WHERE ' . $conds;
        }
        if (!empty($orders)) {
            $query .= ' ORDER BY ' . $orders;
        }
        if (!empty($limit)) {
            $query .= ' LIMIT ' . $start . ', ' . $limit;
        }
        $query = $this->db->query($query);
        $list_banner = array();
        foreach ($query->result() as $row) {
            $list_banner[] = $row;
        }
        $query->free_result();
        return $list_banner;
    }

    public function add_banner($data) {
        $data = $this->auto_render($data);
        if (empty($data['created_at'])) {
            $data['created_at'] = time();
        }
        $id = false;
        if (empty($data['error'])) {
            $this->db->insert($this->_instance['table'], $data);
            $id = mysql_insert_id();
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $id;
    }

    public function update_banner($id, $data) {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        $data['user_id'] = $CI->session->userdata('admin_id');
        if (empty($data['error'])) {
            //Update DB
            $this->db->where('id', $id);
            $this->db->update($this->_instance['table'], $data);
            //Update Memcache
            $CI->st_registry->deleteKey('site/banner_by_id/' . $id);
            if (!empty($data['alias'])) {
                $CI->st_registry->deleteKey('site/banner_by_alias/' . $data['alias']);
            }
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function load_by_id($id, $reload = true) {
        $CI =& get_instance();
        if ($reload) {
            $query = $this->db->get_where($this->_instance['table'], array('id' => $id), 1, 0);
            $this->_data = $query->row();
            $CI->st_registry->setKey('site/banner_by_id/' . $id, $this->_data, CACHE_TIMEOUT);
            $query->free_result();
        } else {
            $this->_data = $CI->st_registry->getKey('site/banner_by_id/' . $id);
            if (!$this->_data) {
                return $this->load_by_id($id, true);
            }
        }
        return $this->_data;
    }

    public function load_by_alias($alias, $reload = true) {
        $CI =& get_instance();
        if ($reload) {
            $query = $this->db->get_where($this->_instance['table'], array('alias' => $alias), 1, 0);
            $this->_data = $query->row();
            $this->_data = $this->auto_render_load($this->_data);
            $CI->st_registry->setKey('site/banner_by_alias/' . $alias, $this->_data, CACHE_TIMEOUT);
            $query->free_result();
        } else {
            $this->_data = $CI->st_registry->getKey('site/banner_by_alias/' . $alias);
            if (!$this->_data) {
                return $this->load_by_alias($alias, true);
            }
        }
        return $this->_data;
    }

    // Check Exists Product By Field, Value
    public static function exists_banner($field, $value, $reload = false) {
        $CI =& get_instance();
        if ($reload) {
            $query = $CI->db->get_where($CI->_instance['table'], array($field => $value), 1, 0);
            $data = $query->row();
            if (!empty($data) && intval($data->id) > 0) {
                $query->free_result();
                $CI->st_registry->setKey('site/banner_by_' . $field . '/' . $value, $data, CACHE_TIMEOUT);
                return $data;
            }
        } else {
            $data = $CI->st_registry->getKey('site/banner_by_' . $field . '/' . $value);
            if (empty($data)) {
                return $CI->exists_banner($field, $value, true);
            }
        }
        return false;
    }

    public function get_data($data, $field) {
        if (is_string($data) && strstr($data, ':')) {
            $data = unserialize($data);
        }
        if (!is_array($data)) {
            return false;
        }
        if (isset($data[$field])) {
            return $data[$field];
        }
        return false;
    }

    public function delete_banner($id) {
        $file = $this->get_file($id);
        @unlink($file);
        $this->db->delete($this->_instance['table'], array('id' => $id));
        // Delete From Memcache
        $CI =& get_instance();
        $CI->st_registry->deleteKey('site/banner_by_id/' . $id);
        return $this;
    }

    private function auto_render($data) {
        $alias = renderToAlias($data['name']);
        if (empty($data['alias'])) {
            $data['alias'] = $alias;
        } else {
            $data['alias'] = renderToAlias($data['alias']);
        }
        $data['updated_at'] = time();
        if (empty($data['is_visible'])) {
            $data['is_visible'] = 0;
        }
        if (!empty($data['visible_from'])) {
            $data['visible_from'] = get_time_stamp($data['visible_from']);
        }
        if (!empty($data['visible_to'])) {
            $data['visible_to'] = get_time_stamp($data['visible_to']);
        }
        return $data;
    }

    public static function get_edit_url($id = null) {
        if (empty($id)) {
            return site_url('admin/site/banner/edit/', true);
        }
        return site_url('admin/site/banner/edit/id/' . $id, true);
    }

    public function get_banner_by_ids($ids, $limit) {
        if (empty($ids) || empty($limit)) {
            return null;
        }
        $query = 'SELECT * FROM '.$this->_instance['table'].' WHERE id IN ('.$ids.') ORDER BY sort_order DESC LIMIT 0, ' . $limit;
        $rs = $this->db->query($query);
        $collection = $rs->result();
        $rs->free_result();
        return $collection;
    }

    public function get_display_mode() {
        return $this->_display;
    }

    public function get_target_mode() {
        return $this->_target;
    }

    public function get_file($id) {
        $query = 'SELECT file FROM '.$this->_instance['table'].' WHERE id = ' . $id . ' ORDER BY id DESC LIMIT 0, 1';
        $rs = $this->db->query($query);
        $row = $rs->row();
        $rs->free_result();
        return $row->file;
    }

    public function get_display_class($display) {
        if (!isset($this->_display_class[$display])) {
            return 'cl';
        }
        return $this->_display_class[$display];
    }
}

?>